import { Card, Button, Checkbox, Form, Input, message } from 'antd'
import '@/pages/Login/index.scss'
import logo from '@/assets/login.jpg'
import { useStore } from '@/store'
import { useNavigate } from 'react-router-dom'
function Login () {
  // 获取跳转实例对象
  const navigate = useNavigate()
  const { loginStore } = useStore()
  // 登录格式正确成功回调事件
  const onFinish = async (values) => {
    await loginStore.getTokenZ({
      mobile: values.username,
      code: values.password
    })
    if (loginStore.token !== '') {
      navigate('/')
      message.success('登录成功')
    } else {
      message.error('登录失败')
    }
  }

  return (
    <div className="login">
      <Card className="login-container">
        <img className="login-logo" alt="" src={logo} />
        {/* 登录输入表单 */}
        <Form
          validateTrigger={['onBlur', 'onChange']}
          onFinish={onFinish}
          initialValues={{
            mobile: '13911111111',
            code: '246810',
            remember: true
          }}
        >
          <Form.Item
            name="username"
            rules={[
              {
                required: true,
                message: '请输入手机号!',
              },
              {
                pattern: /^\w{4,16}$/,
                message: '请输入正确的账号格式',
                validateTrigger: 'onBlur'
              }
            ]}>
            <Input size="large" placeholder="请输入验证码" />
          </Form.Item>
          <Form.Item
            name="password"
            rules={[
              {
                required: true,
                message: '请输入密码!',
              },

              {
                len: 6,
                message: '验证码6个字符',
                validateTrigger: 'onBlur'
              },


            ]}>
            <Input.Password size="large" placeholder="请输入密码" />
          </Form.Item>
          <Form.Item name="remember"
            valuePropName="checked">
            <Checkbox className="login-checkbox-label">我已阅读并同意「用户协议」和「隐私条款」</Checkbox>
          </Form.Item>
          <Form.Item >
            <Button type="primary" htmlType="submit" size="large" block >
              登录
            </Button>
          </Form.Item>
        </Form>
      </Card>
    </div >
  )
}
export default Login